package cn.yhjz.platform.system.config.security;

import cn.yhjz.platform.system.constant.Constant;
import cn.yhjz.platform.system.entity.LogedUserDetails;
import cn.yhjz.platform.system.entity.SysUser;
import cn.yhjz.platform.system.entity.SysUserRole;
import cn.yhjz.platform.system.mapper.SysMenuMapper;
import cn.yhjz.platform.system.mapper.SysUserMapper;
import cn.yhjz.platform.system.mapper.SysUserRoleMapper;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser paramUser = new SysUser();
        paramUser.setUsername(username);
        SysUser sysUser = sysUserMapper.selectOne(paramUser);
        if (sysUser == null) {
            return null;
        }
        //查询用户的角色
        QueryWrapper userRoleQueryWrapper = new QueryWrapper();
        SysUserRole paramUserRole = new SysUserRole();
        paramUserRole.setUserCode(sysUser.getUserCode());
        userRoleQueryWrapper.setEntity(paramUserRole);
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectList(userRoleQueryWrapper);
        //获得角色列表
        List<String> roleCodeList = new ArrayList<>();
        for (SysUserRole sysUserRole : sysUserRoleList) {
            roleCodeList.add(sysUserRole.getRoleCode());
        }
        //获得权限列表
        List<String> permissionCodeList;
        //如果是管理员，就返回所有权限
        if (Constant.ADMIN_USER_NAME.equals(sysUser.getUsername())) {
            permissionCodeList = this.sysMenuMapper.getAllPermissionCodeByRoleCodes();
        } else {
            if (CollUtil.isNotEmpty(roleCodeList)) {
                permissionCodeList = this.sysMenuMapper.getPermissionCodeByRoleCodes(roleCodeList);
            } else {
                permissionCodeList = new ArrayList<>();
            }
        }
        Set<GrantedAuthority> grantedAuthorityList = new HashSet<>();
        for (String permissionCode : permissionCodeList) {
            grantedAuthorityList.add(new SimpleGrantedAuthority(permissionCode));
        }
        /* // 使用springsecurity自带的User类
        User.UserBuilder userBuilder = User
                .builder()
                .username(sysUser.getUsername())
                .password(sysUser.getPassword());//"{noop}" +
        userBuilder.authorities(grantedAuthorityList);
        UserDetails user = userBuilder.build();

        return user;
        */

        //使用自定义的User类
        LogedUserDetails logedUserDetails = new LogedUserDetails();
        logedUserDetails.setUsername(sysUser.getUsername());
        logedUserDetails.setPassword(sysUser.getPassword());
        logedUserDetails.setAuthorities(grantedAuthorityList);

        return logedUserDetails;
    }
}
